﻿// --------------------------------
// <copyright file="MenuHelper.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>14. 2. 2012 0:13:46</date>

// <summary>
// 
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FITClub.Util;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace FITClub.TextureMenu
{
    public class MenuHelper
    {
        private static Texture2D fillTexture;

        public static Texture2D FillTexture
        {
            get
            {
                if (fillTexture == null)
                {
                    fillTexture = CreateFillTexture(255);
                }
                return fillTexture;
            }
        }

        public static void DrawText(SpriteBatch spriteBatch, SpriteFont font, string text, Color textColor, Rectangle bounds, TextAlignment alignment, bool rescale)
        {
            Vector2 scale = (rescale) ? new Vector2((float)MenuManager.ScaleX, (float)MenuManager.ScaleY) : Vector2.One;
            int width = (int)(font.MeasureString(text).X * scale.X);
            Vector2 vText = font.CenterString(text, bounds, scale.X, scale.Y);
            switch (alignment)
            {
                case TextAlignment.Left:
                    if (width <= bounds.Width)
                    {
                        vText.X = bounds.X;
                    }
                    else
                    {
                        vText.X = bounds.X + bounds.Width - width;
                    }
                    break;
                case TextAlignment.Right:
                    if (width <= bounds.Width)
                    {
                        vText.X = bounds.X + bounds.Width - width;
                    }
                    else
                    {
                        vText.X = bounds.X;
                    }
                    break;
            }
            spriteBatch.DrawString(font, text, vText, textColor, 0, Vector2.Zero, scale.Y, SpriteEffects.None, 0);
        }

        /// <summary>
        /// Create 1x1 pixel long color texture.
        /// </summary>
        /// <param name="alpha"></param>
        /// <returns></returns>
        public static Texture2D CreateFillTexture()
        {
            return CreateFillTexture(Color.White, MenuManager.Current.GraphicsDevice);
        }

        /// <summary>
        /// Create 1x1 pixel long color texture.
        /// </summary>
        /// <param name="alpha"></param>
        /// <returns></returns>
        public static Texture2D CreateFillTexture(byte alpha)
        {
            return CreateFillTexture(new Color(255, 255, 255, alpha), MenuManager.Current.GraphicsDevice);
        }

        /// <summary>
        /// Create 1x1 pixel long color texture.
        /// </summary>
        /// <param name="alpha"></param>
        /// <returns></returns>
        public static Texture2D CreateFillTexture(Color fillColor, GraphicsDevice device)
        {
            Texture2D t = new Texture2D(device, 1, 1);
            t.SetData(new Color[] { Color.FromNonPremultiplied(fillColor.R, fillColor.G, fillColor.B, fillColor.A) });
            return t;
        }

        /// <summary>
        /// Create 1x1 pixel long color texture.
        /// </summary>
        /// <param name="alpha"></param>
        /// <returns></returns>
        public static Texture2D CreateFillTexture(Color fillColor)
        {
            return CreateFillTexture(fillColor, MenuManager.Current.GraphicsDevice);
        }

        public static void FillRectangle(SpriteBatch spriteBatch, Texture2D texture, Rectangle bounds, Color fillColor)
        {
            spriteBatch.Draw(texture, bounds, fillColor);
        }

        public static void FillRectangle(SpriteBatch spriteBatch, Rectangle bounds, Color fillColor)
        {
            spriteBatch.Draw(MenuHelper.FillTexture, bounds, fillColor);
        }

        public static void DrawLine(SpriteBatch spriteBatch, Vector2 p1, Vector2 p2, int height, Color fill)
        {
            float angle = (float)Math.Atan2(p1.Y - p2.Y, p1.X - p2.X);
            int distance = (int)Math.Round(Vector2.Distance(p1, p2));
            spriteBatch.Draw(FillTexture, new Rectangle((int)p2.X, (int)p2.Y, distance, height), null, fill, angle, Vector2.Zero, SpriteEffects.None, 0);

            spriteBatch.Draw(FillTexture, new Rectangle((int)p2.X, (int)p2.Y, 20, height - 1), null, fill, angle + (MathHelper.PiOver4 / 2f), Vector2.Zero, SpriteEffects.None, 0);
            spriteBatch.Draw(FillTexture, new Rectangle((int)p2.X, (int)p2.Y, 20, height - 1), null, fill, angle - (MathHelper.PiOver4 / 2f), Vector2.Zero, SpriteEffects.None, 0);
        }

        public static void DrawBorder(SpriteBatch spriteBatch, Texture2D texture, Rectangle rect, Color color, int width)
        {
            FillRectangle(spriteBatch, texture, new Rectangle(rect.X, rect.Y - width, rect.Width, width), color);
            FillRectangle(spriteBatch, texture, new Rectangle(rect.X, rect.Bottom, rect.Width, width), color);

            FillRectangle(spriteBatch, texture, new Rectangle(rect.X - width, rect.Y - width, width, rect.Height + 2 * width), color);
            FillRectangle(spriteBatch, texture, new Rectangle(rect.Right, rect.Y - width, width, rect.Height + 2 * width), color);
        }

        public static void DrawBorder(SpriteBatch spriteBatch, Rectangle rect, Color color, int width)
        {
            MenuHelper.DrawBorder(spriteBatch, MenuHelper.FillTexture, rect, color, width);
        }

        public  static SpriteFont LoadFontOrDefault(ContentManager content, string fontpath)
        {
            if (fontpath != null && fontpath.Trim() != string.Empty)
            {
                return content.Load<SpriteFont>(fontpath);
            }
            return MenuManager.Current.NormalFont;
        }
    }
}